package com.apollographql.apollo.internal.interceptor;

import com.apollographql.apollo.api.internal.Utils;
import com.apollographql.apollo.interceptor.ApolloInterceptor;
import com.apollographql.apollo.interceptor.ApolloInterceptorChain;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.Executor;

public final class RealApolloInterceptorChain implements ApolloInterceptorChain {
    private final int interceptorIndex;
    private final List<ApolloInterceptor> interceptors;

    public RealApolloInterceptorChain(List<ApolloInterceptor> interceptors2) {
        this(interceptors2, 0);
    }

    private RealApolloInterceptorChain(List<ApolloInterceptor> interceptors2, int interceptorIndex2) {
        if (interceptorIndex2 <= interceptors2.size()) {
            this.interceptors = new ArrayList((Collection) Utils.checkNotNull(interceptors2, "interceptors == null"));
            this.interceptorIndex = interceptorIndex2;
            return;
        }
        throw new IllegalArgumentException();
    }

    @Override // com.apollographql.apollo.interceptor.ApolloInterceptorChain
    public void proceedAsync(ApolloInterceptor.InterceptorRequest request, Executor dispatcher, ApolloInterceptor.CallBack callBack) {
        if (this.interceptorIndex < this.interceptors.size()) {
            this.interceptors.get(this.interceptorIndex).interceptAsync(request, new RealApolloInterceptorChain(this.interceptors, this.interceptorIndex + 1), dispatcher, callBack);
            return;
        }
        throw new IllegalStateException();
    }

    @Override // com.apollographql.apollo.interceptor.ApolloInterceptorChain
    public void dispose() {
        for (ApolloInterceptor interceptor : this.interceptors) {
            interceptor.dispose();
        }
    }
}
